//
//  FilterView.m
//  CloudMD
//
//  Created by centling on 16/3/14.
//  Copyright © 2016年 HuaZhengInfo. All rights reserved.
//

#import "FilterView.h"
#import "FilterOptionView.h"

@implementation FilterView

+ (instancetype)filterView {
    return [[[NSBundle mainBundle] loadNibNamed:@"FilterView" owner:self options:nil] lastObject];
}

- (void)awakeFromNib {
    UILabel *stateLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 80, 30)];
    stateLabel.text = @"订单状态";
    stateLabel.textColor = RED;
    [self.scrollView addSubview:stateLabel];
    FilterOptionView *stateOptionView = [[FilterOptionView alloc] init];
    NSArray *stateArray = @[@"不限", @"未完成", @"已完成"];
    [stateOptionView zhaoaddButtonWithViewWidth:SCR_W-40 array:stateArray];
    stateOptionView.frame = CGRectMake(20, CGRectGetMaxY(stateLabel.frame)+10, SCR_W-20, stateOptionView.sumHeight);
    [self.scrollView addSubview:stateOptionView];
    
    UILabel *cateLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(stateOptionView.frame)+15, 80, 30)];
    cateLabel.text = @"问题类型";
    cateLabel.textColor = RED;
    [self.scrollView addSubview:cateLabel];
    NSUserDefaults *cateDefaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray *mutArray = [cateDefaults objectForKey:@"cateDefaults"];
    NSMutableArray *cateArray = [[NSMutableArray alloc] init];
    for (NSDictionary * dataDict in mutArray) {
        GetWGHModel * model = [[GetWGHModel alloc] initWithDict:dataDict];
        [cateArray addObject:model.TEXT];
    }
    [cateArray insertObject:@"不限" atIndex:0];
    FilterOptionView *cateOptionView = [[FilterOptionView alloc] init];
    cateOptionView.multipleSelect = YES;
    [cateOptionView zhaoaddButtonWithViewWidth:SCR_W-40 array:cateArray];
    cateOptionView.frame = CGRectMake(20, CGRectGetMaxY(cateLabel.frame)+10, SCR_W-20, cateOptionView.sumHeight);
    [self.scrollView addSubview:cateOptionView];
    
    UILabel *orderNumberLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(cateOptionView.frame)+15, 80, 30)];
    orderNumberLabel.text = @"订单号";
    orderNumberLabel.textColor = RED;
    [self.scrollView addSubview:orderNumberLabel];
    UITextField *orderTf = [[UITextField alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(orderNumberLabel.frame)+10, SCR_W-60, 30)];
    orderTf.keyboardType = UIKeyboardTypeASCIICapable;
    orderTf.layer.masksToBounds = YES;
    orderTf.layer.borderColor = [UIColor lightGrayColor].CGColor;
    orderTf.layer.borderWidth = 1;
    orderTf.layer.cornerRadius = 5;
    [self.scrollView addSubview:orderTf];
    
    UIButton *doneButton = [[UIButton alloc] init];
    doneButton.frame = CGRectMake(0, 0, SCR_W/2, 40);
    doneButton.center = CGPointMake(SCR_W * 0.5 - 10, CGRectGetMaxY(orderTf.frame)+ 40);
    [doneButton setBackgroundImage:[UIImage imageNamed:@"3在线下单"] forState:UIControlStateNormal];
    [doneButton setTitle:@"开始筛选" forState:UIControlStateNormal];
    [doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    doneButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [doneButton addTarget:self action:@selector(filterClick) forControlEvents:UIControlEventTouchUpInside];
    [self.scrollView addSubview:doneButton];

    self.scrollView.contentSize = CGSizeMake(SCR_W-20, CGRectGetMaxY(doneButton.frame)+30);
}

- (void)filterClick {
    if ([self.delegate respondsToSelector:@selector(filterActionWithStateArray:cateArray:orderString:)]) {
        FilterOptionView *firstOptionView = [self.scrollView subviews][1];
        FilterOptionView *secondOptionView = [self.scrollView subviews][3];
        UITextField *orderTextField = [self.scrollView subviews][5];
        [self.delegate filterActionWithStateArray:firstOptionView.keyWordsArray cateArray:secondOptionView.keyWordsArray orderString:orderTextField.text];
        
    }
}


@end
